package com.ruoyi.web.controller.teaching;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.teaching.domain.*;
import com.ruoyi.teaching.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教务班级Controller
 *
 * @author ruoyi
 * @date 2022-10-12
 */
@Controller
@RequestMapping("/teaching/classes")
public class TeachingClassesController extends BaseController {
    private String prefix = "teaching/classes";

    @Autowired
    private ITeachingClassesService teachingClassesService;

    @Autowired
    private ITeachingStudentApplyService teachingStudentApplyService;

    @Autowired
    private ITeachingCourseService teachingCourseService;

    @Autowired
    private ITeachingCourseFolderService folderService;

    @Autowired
    private ITeachingSchoolService teachingSchoolService;

    @Autowired
    private ITeachingClassesStudentsService teachingClassesStudentsService;

    //    @RequiresPermissions("teaching:classes:view")
    @GetMapping()
    public String classes() {
        return prefix + "/classes";
    }

    /**
     * 查询教务班级列表
     */
//    @RequiresPermissions("teaching:classes:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingClasses teachingClasses) {
        startPage();
        teachingClasses.setSchoolId(getSysUser().getSchoolId());
        List<TeachingClasses> list = teachingClassesService.selectTeachingClassesList(teachingClasses);
        for (TeachingClasses classes : list) {
            Long schoolId = classes.getSchoolId();
            classes.setTeachingSchool(teachingSchoolService.selectTeachingSchoolBySchoolId(schoolId));
        }
        return getDataTable(list);
    }

    /**
     * 导出教务班级列表
     */
//    @RequiresPermissions("teaching:classes:export")
    @Log(title = "教务班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingClasses teachingClasses) {
        List<TeachingClasses> list = teachingClassesService.selectTeachingClassesList(teachingClasses);
        ExcelUtil<TeachingClasses> util = new ExcelUtil<TeachingClasses>(TeachingClasses.class);
        return util.exportExcel(list, "教务班级数据");
    }

    /**
     * 新增教务班级
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        Long schoolId = getSysUser().getSchoolId();
        mmap.put("schoolId", schoolId);
        mmap.put("courses", teachingCourseService.selectTeachingCourseAll());
        return prefix + "/add";
    }

    /**
     * 新增保存教务班级
     */
//    @RequiresPermissions("teaching:classes:add")
    @Log(title = "教务班级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingClasses teachingClasses) {
        teachingClasses.setCreateId(getUserId());
        teachingClasses.setCreateBy(getLoginName());
        teachingClasses.setCreateTime(new Date());
        //新增班级时候需要设置默认需要的章节数量
        String ancestors = folderService.getNeedSections(teachingClasses.getCourseId(), 3);
        teachingClasses.setCurrentFolderAncestors(ancestors);
        return toAjax(teachingClassesService.insertTeachingClasses(teachingClasses));
    }

    /**
     * 修改教务班级
     */
//    @RequiresPermissions("teaching:classes:edit")
    @GetMapping("/edit/{classId}")
    public String edit(@PathVariable("classId") Long classId, ModelMap mmap) {
        TeachingClasses teachingClasses = teachingClassesService.selectTeachingClassesByClassId(classId);
        mmap.put("teachingClasses", teachingClasses);
        return prefix + "/edit";
    }

    /**
     * 修改保存教务班级
     */
//    @RequiresPermissions("teaching:classes:edit")
    @Log(title = "教务班级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingClasses teachingClasses) {
        teachingClasses.setUpdateBy(getLoginName());
        teachingClasses.setUpdateTime(new Date());
        return toAjax(teachingClassesService.updateTeachingClasses(teachingClasses));
    }

    /**
     * 删除教务班级
     */
//    @RequiresPermissions("teaching:classes:remove")
    @Log(title = "教务班级", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        for (Long classId : Convert.toLongArray(ids)) {
            TeachingClassesStudents param = new TeachingClassesStudents();
            param.setClassId(classId);
            List<TeachingClassesStudents> list = teachingClassesStudentsService.selectTeachingClassesStudentsList(param);
            if (StringUtils.isNotEmpty(list)) {
                return error("班级删除失败，原因为该班级已有报读学员");
            }
        }
        return toAjax(teachingClassesService.deleteTeachingClassesByClassIds(ids));
    }

    /**
     * 班级详情
     */
    @GetMapping("/info/{classId}")
    public String Info(@PathVariable("classId") Long classId, ModelMap mmap) {
        TeachingClasses teachingClasses = teachingClassesService.selectTeachingClassesByClassId(classId);
        mmap.put("teachingClasses", teachingClasses);
        return prefix + "/info";
    }

    /**
     * 三级联动
     *
     * @return
     */
    @GetMapping("/select")
    public String select() {
        return prefix + "/select";
    }

    /**
     * 添加学生进班级
     */
    @GetMapping("/addStudent/{classId}")
    public String addStudent(@PathVariable("classId") Long classId, ModelMap mmap) {
        TeachingClasses teachingClasses = teachingClassesService.selectTeachingClassesByClassId(classId);
        mmap.put("teachingClasses", teachingClasses);
        return prefix + "/addStudent";
    }

    /**
     * 调班界面
     */
    @GetMapping("/chgClass/{classStudentId}")
    public String chgClass(@PathVariable("classStudentId") Long classStudentId, ModelMap mmap) {
        TeachingClassesStudents classesStudents = teachingClassesStudentsService.selectTeachingClassesStudentsByClassStudentId(classStudentId);
        Long classId = classesStudents.getClassId();
        TeachingClasses teachingClasses = teachingClassesService.selectTeachingClassesByClassId(classId);
        Long applyId = classesStudents.getApplyId();
        TeachingStudentApply studentApply = teachingStudentApplyService.selectTeachingStudentApplyByApplyId(applyId);
        //找出当前账号所在学校的所有班级
        TeachingClasses param = new TeachingClasses();
        param.setSchoolId(getSysUser().getSchoolId());
        param.setCourseId(studentApply.getCourseId());
        List<TeachingClasses> classes = teachingClassesService.selectTeachingClassesList(param);
        mmap.put("teachingStudentApply", studentApply);
        mmap.put("teachingClassesStudents", classesStudents);
        mmap.put("teachingClasses", teachingClasses);
        mmap.put("classList", classes);
        return prefix + "/chgClass";
    }

    @PostMapping("/endClass")
    @ResponseBody
    public AjaxResult endClass(Long classId){
        TeachingClasses teachingClasses = teachingClassesService.selectTeachingClassesByClassId(classId);
        teachingClasses.setDelFrag("2");
        return toAjax(teachingClassesService.updateTeachingClasses(teachingClasses));
    }
}
